//
//  ChangeNameController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/12.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "ChangeNameController.h"
#import "Config.h"
#import "User.h"

@interface ChangeNameController () <UITextFieldDelegate, UIActionSheetDelegate>
{
    User *_user;
    NSArray *_pickerDataArray;
}
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *sexField;

@end

@implementation ChangeNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"真实姓名";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"修改" style:UIBarButtonItemStyleDone target:self action:@selector(changeInfoEvent)];
    _user = [NSKeyedUnarchiver unarchiveObjectWithFile:XHUserPath];
    if (_user) {
        self.nameField.placeholder = _user.name;
        self.sexField.text = _user.sex;
    }
    [self.nameField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 修改
- (void)changeInfoEvent {
    NSString *name = [self.nameField.text stringRemoveSpecialSymbol:@" "];
    NSString *sex = self.sexField.text == nil ? @"" : self.sexField.text;
    if (name.length < 1)
        [self setAlertViewWithMessage:@"请输入真实姓名!"];
    else if ([name isEqualToString:self.nameField.placeholder])
        [self setAlertViewWithMessage:@"并无修改"];
    else {
        [self.view endEditing:NO];
        NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
        NSDictionary *param = @{@"name" : name, @"sex" : sex};
        __weak typeof(self) weakSelf = self;
        [HTTPSTool POSTRequestWithPath:XHEditUserInfo cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//            XHLog(@"修改用户信息 -> JSON: %@", JSON)
            _user.name = name;
            _user.sex = sex;
            [NSKeyedArchiver archiveRootObject:_user toFile:XHUserPath];
            if ([_delegate respondsToSelector:@selector(changeName:changeNameWithName:)])
                [_delegate changeName:self changeNameWithName:name];
            [ZYToast showOperationSuccess];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
}

#pragma mark - textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == self.nameField) return YES;
    [self.nameField resignFirstResponder];
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择性别:" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男", @"女", nil];
    [sheet showInView:self.view];
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - ActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *text = [actionSheet buttonTitleAtIndex:buttonIndex];
    if (![text isEqualToString:@"取消"]) {
        self.sexField.text = text;
    }
}

#pragma mark - AlertView
- (void)setAlertViewWithMessage:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提 示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:defaultAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:NO];
}

@end
